** This file compiles the village data for Ghana and replicates Nunn's regressions

use "Appendix Table A12\afro_ghana_round5_geo.dta", replace // Used ARCmap Generate near table function

merge m:1 gridcell using "Appendix Table A12\grid_ghana.dta"
keep if _merge==3
drop _merge

tempfile villagedata
gen n=1

* Generate Number of missions within 25 km radius of the village
// a) Roome missions, Beach missions
// b) Our missions
collapse (sum) loc_roomemissions=roome24_mis_num loc_beachmissions=station_beach loc_ourmissions=missions_num loc_ourmissions01=missions_yn  ///
(mean) longitude latitude ///
(max) period1_25dummy=period1 period2_25dummy=period2 mainstat_yn_25dummy=mainstat_yn asstschools_yn_25dummy=asstschools_yn ///
eurononmain4690_25dummy=eurononmain4690 euromain4690_25dummy=euromain4690 ///
loc_cells_river=dist2riv_navigable10 ///
(sum)  loc_ncells=n ///
, by(respno)

keep respno loc* latitude longitude period1_25dummy period2_25dummy mainstat_yn_25dummy asstschools_yn_25dummy eurononmain4690_25dummy euromain4690_25dummy

rename latitude latitude_respno
rename longitude longitude_respno
label var loc_roomemissions "Number of Roome missions in 25 km distance of Afrobarometer village location"
label var loc_ourmissions "Number of Blue Book missions in 25 km distance of Afrobarometer village location"
label var loc_ourmissions01 "Number of cells with at least one Book missions in 25 km distance of Afrobarometer village location"

* `type' variables
* period1 period2 mainstat_yn asstschools_yn eurononmain4690 euromain4690
label var period1_25dummy "At least one cell within 25km where created mission 1751-1850"
label var period2_25dummy "At least one cell within 25km where created mission 1851-1875"
label var mainstat_yn_25dummy "At least one cell within 25km with main station 1924"
label var asstschools_yn_25dummy "At least one cell within 25km with school 1924"
label var eurononmain4690_25dummy "At least one cell within 25km with euro residence 1924"
label var euromain4690_25dummy "At least one cell within 25km with euro visit 1924"

save `villagedata', replace // contains number of missions in 25 km radius of respondent, respno as identifier


********************************************************************************
** Dataset of respno and gridcells
use "Appendix Table A12\afro_ghana_round5_geo.dta", replace
egen min_dist = min(dist2grid), by(respno)
gen incell=(dist2grid==min_dist)
drop min_dist
keep if incell==1 // keep only the obs in which the grid is located

* Merge with Ghana data - each obs is a grid
merge m:1 gridcell using "Appendix Table A12\grid_ghana.dta"
keep if _m==3 // Keep only those grids in which respno are located
drop _m

** Merge with mission stations data that was generated (number of missions in 25 km distance)
merge 1:1 respno using `villagedata' // data of grids within 25 km radius
keep if _m==3
drop _m

duplicates drop respno, force

* Standardise variable names
* gen loc_explorer_contact=(dist2explorer10==1)
* gen loc_cells_river=(dist2river<=10) // 25 km ???
gen dist2lake10km=0 // no lakes in Ghana
* loc_ln_export_area // exists in data set
* loc_altitude // alt_mean
gen loc_explorer_contact=(dist2explorer<=12)
* gen railway_contact=(dist2rail1900<=12)
* gen railway_contact_dissect=(railway_contact==1 & incell==1)
gen loc_railway_contact_dissect97=0  // no railway in Ghana in 1897


********************************************************************************
** Merge with Afrobarometer 5 data **
merge 1:1 respno using "Appendix Table A12\Merged R5.fulldata.dta", force

keep if _m==3
drop _merge

*** Generate individual data
gen age=q1 if q1>0 & q1<990
gen age2=age*age
gen male=(q101==1)
gen urban_dum=(urbrur==1)

keep if q102==1 // Respondent’s race=White/European

gen education=q97 
replace education=. if education==-1 | education>13

gen dist2_dist2explorer_25=(dist2explorer<=25)
********************************************************************************

replace eurononmain4690=0 if eurononmain4690==.
replace euromain4690=0 if euromain4690==.
gen dist2river_nav10km=(dist2riv_navigable<=10)

local control_individual "age age2 male urban_dum"
local control_village "loc_explorer_contact dist2river_nav10km dist2lake10km loc_railway_contact_dissect97 ln_export_area soil5 alt_mean" // Village level variables -> Is ln_export_area village level?

local stdctrls "soil5 dist2riv_navigable10 ln_export_area prec_mean dist2coast histo_malaria city1800 density1800m"
local ssactrls1900 "soil5 prec_mean ldist2coast histo_malaria tsi city1800 yrstudy_yn yrstudy1900 missing_murdock nevercolonised date_col centra ldist2muslimcentre port1850_yn dist2riv_navigable10 ldist2explorer dist2rail30 density1800m city10k_1900 lurbc1900 lrurc1900 slavery ln_export_area lgridvalue1900 minet_dist50 polygamy"
local ssactrls1924 "soil5 prec_mean ldist2coast histo_malaria tsi city1800 yrstudy_yn yrstudy1924 missing_murdock nevercolonised date_col centra ldist2muslimcentre port1850_yn dist2riv_navigable10 ldist2explorer dist2rail30 density1800m city10k_1900 lurbc1900 lrurc1900 slavery ln_export_area lgridvalue1924 minet_dist50 polygamy"
local ourctrls "prec_mean alt_mean alt_sd soil5 histo_malaria port1850_yn ldist2coast nongcc1873 dist2riv_navigable10 traderoute18501890_dist10 exploroute188089_dist10 dist2rail193210 dist2class123_193010 city1800 map08_yn national headchief lupop_1891 lupop_1901 lrpop_1901 lupop_1931 lrpop_1931 ln_export_area slavemarket_strict_dist50 palmoil1900_dist50 kolanut0 rubber1900_dist50 cocoa minet_dist1932_50"

local type "period1 period2 mainstat_yn asstschools_yn eurononmain4690 euromain4690"

keep respno gridcell education missions_yn roome24_mis_yn loc_roomemissions loc_ourmissions latitude longitude area_sqkm alt_sd nongcc1873 `control_individual' `control_village' `stdctrls' `ssactrls1924' `ourctrls' `type' year period1_25dummy period2_25dummy mainstat_yn_25dummy asstschools_yn_25dummy eurononmain4690_25dummy euromain4690_25dummy loc_ncells


label var missions_yn "Mission in grid"
label var mainstat_yn "Main station in grid"
label var dist2class123_193010 ""
label var euromain4690 "European main station 1846-1890"
label var eurononmain4690 "European outstation 1846-1890"
label var lupop_1931 "Log urban population 1931"
label var lrpop_1931 "Log rural population 1931"
label var lupop_1901 "Log urban population 1901"
label var lrpop_1901 "Log rural population 1901"
label var lupop_1891 "Log urban population 1891"
label var asstschools_yn "Assisted school in grid"
label var soil5 "Soil quality index (1-5)"
label var density1800m "Population density 1800"
label var city1800 "City in grid 1800"
label var national "National city in grid"
label var nongcc1873 "Outside Gold Coast Colony border of 1873"
label var ldist2coast "log distance to coast (km)"
label var dist2riv_navigable10 "Navigable river within 10km"
label var traderoute18501890_dist10 "Trade route within 10km" 
label var exploroute188089_dist10 "European explorer route within 10km"
label var dist2rail193210 "Railroad within 10km 1932"
label var dist2class123_193010 ""
label var ldist2muslimcentre "Log distance to Muslim centre"
label var ldist2explorer "Log distance to European xplorer route"
label var lurbc1900 "Log urban head count 1900 (HYDE)"
label var lrurc1900 "Log rural head count 1900 (HYDE)"
label var lgridvalue1924 "Log grid value 1924"
label var minet_dist50 "Mine within 50km 1924"
label var city10k_1900 "City above 10k in 1900"
label var dist2rail30 "Railroad within 30km"
label var period1 ""
label var period2 ""
label var loc_explorer_contact "Grid dissected by European explorer route"
label var loc_ncells "Number of cells wihtin 25km"
label var rubber1900_dist50 "Rupper within 50km 1900"
label var palmoil1900_dist50 "Palmoil plantation within 50km 1900"
label var slavemarket_strict_dist50 "Slave market (strict) within 50km"
label var minet_dist1932_50 "Mine within 50km 1932"
label var kolanut0 "Cell belongs to a kolaproducing area"
label var dist2class123_193010 "Grid within 10km of 1930 class 1,2,3 road"

save "Appendix Table A12\replicating_nunn_ghana.dta", replace